package tech.aistar.ajax;

import com.alibaba.fastjson.JSONArray;
import tech.aistar.prj.entity.Pet;
import tech.aistar.prj.entity.Result;
import tech.aistar.prj.service.IPetService;
import tech.aistar.prj.service.impl.PetServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 本类功能:
 *
 * @author cxylk
 * @date 2020/9/26 16:19
 */
@WebServlet(urlPatterns = "/json/update")
public class UpdateAction extends HttpServlet {
    private IPetService petService=new PetServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json;charset=utf-8");
        PrintWriter out=resp.getWriter();
        //String petAge=req.getParameter("age");
        String petId=req.getParameter("id");
        String petName=req.getParameter("name");
        Pet pet=petService.findById(Integer.valueOf(petId));
        //pet.setAge(Integer.valueOf(petAge));
        pet.setName(petName);
        int count=petService.update(pet);
        if(count!=0){
            out.write(JSONArray.toJSONString(new Result("200","更新成功")));
        }else{
            out.write(JSONArray.toJSONString(new Result("500","更新失败")));
        }
    }
}
